{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Sequential SAR Change Detection"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "55fe532feb2f4bcf80c68adedb0e3ade",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Map(basemap={'url': 'https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', 'max_zoom': 19, 'attribution': 'Map …"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "5313efcf4dec4948aea26aaec40d9b51",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "VBox(children=(Textarea(value='Algorithm output', layout=Layout(width='99%'), rows=4), HBox(children=(Text(val…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "from auxil.eeSar_seq import run\n",
    "run()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# How to use the interface\n",
    "\n",
    "#### To generate change maps:\n",
    "\n",
    "    - select the desired time interval\n",
    "    - use the Polygon or rectangle tool to select an ROI (default: Jülich, Germany and surroundings)\n",
    "    - leave the relative orbit value at 0\n",
    "    - choose orbit pass (ascending or descending), platform (S1a, S1b or both)\n",
    "    - choose change map type for previewing (color-coded time of first change, last change or change frequency) or a bitemporal map for any of the consecutive time intervals\n",
    "    - set a desired opacity for the preview overlay\n",
    "    - leave the significance level at its default value\n",
    "    - select or deselect whether to show initially a minimum cloud cover S2 image. If deselected, a temporal average of all S1 images found is displayed\n",
    "    - press Collect to accumulate the images\n",
    "Information about the images found is shown in the output window and an S2 image or a composite of the mean of all of the images is overlayed onto the map. If more than one relative orbit number is present, you can choose one of them by entering its value into the mask. For change detection, it is advisable not to use more than one orbit. \n",
    "\n",
    "    - press Collect again if desired\n",
    "    - press Preview to see a preview overlay of the chosen change map \n",
    "Depending on the size of the images and their number, this can take up to a few minutes. The overlay is built up tile-by-tile as the GEE servers complete their tasks. Selecting a different change map or opacity does not require a new Run: the Preview button remains activated. You can use a water mask and/or make the no change pixels transparent with the corresponding check boxes. The color coding of the change maps is explained in the output window. Optionally use the URL displayed in the URL window to download the change maps. This may fail for longer time series and generate a corrupted ZIP file. Export to assets or to your Google Drive is reliable. When satisfied, enter username/path in the Export to assets mask or the filename in the export to drive mask. \n",
    "    \n",
    "    - Press ExportToAssets or ExportToDrive to start an export task on the GEE servers\n",
    "An additional metadata file in CSV format is exported to the Google Drive for each asset export. \n",
    "\n",
    "    - Press ExporSeries to export the entire time series\n",
    "\n",
    "This triggers an export of the entire S1 time series subsets as well as the S2 image (if selected) to the Google Drive.\n",
    "\n",
    "You can also analyse your own image collection, if available (e.g. sentinel-1 downloaded from RSA and pre-processed wth the SNAP toolbox or preprocessed RadarSat-2 imagery). Enter the collection name in the input template and the corresponding ENL.\n",
    "\n",
    "#### File structure\n",
    "\n",
    "The exported or downloaded, per-pixel change maps for a time sequence of length k are combined into a single file in GeoTiff format:\n",
    "\n",
    "    band 1 cmap: Period in which most recent significant change occured (integer 1 ... k-1)\n",
    "    band 2 smap: Period in which the first significant change occurred (integer 1 ... k-1)\n",
    "    bans 3 fmap: Number of significant changes (integer 1 ... k-1)\n",
    "    bands 4 ... k+3: Changes in each interval (1=positive definite, 2=negative definite, 3=indefinite)\n",
    "    band k+4: Sentinel 2 background image (band B8) if available, otherwise averaged VV band of the s1 series. (This is only appended on export to assets)\n",
    "    \n",
    "#### Fine adjustments\n",
    "\n",
    "    - select a different significance level\n",
    "The significance level applies to the tests performed in each change interval. They are statistically independent for no-change observations. \n",
    "\n",
    "    -select 3x3 median filter\n",
    "The p-values used in the tests are filtered before testing. This has the effect of supressing single-pixel changes (salt-and-pepper effect).\n",
    "\n",
    "    -select Quick Preview\n",
    "Preview results are calculated at the scale corresponding to the current zooom level. This invalidates the ENL value so that, in general, fewer changes are detected. However calculation time is shortened, which is conveneient for rough data exploration. Unselect in order to force calcuation at the nominal scale of the data.     \n",
    "\n",
    "#### Limits\n",
    "\n",
    "The ROI should be completely enclosed within the S1 full scenes involved in the calculation. The larger the ROI and the longer the time series, the longer the preview and export calculations. By experience, time series longer than about 75 will cause a GEE server internal error (due to stack overflow caused by the depth of iteration).\n",
    "\n",
    "__Reference:__ Knut Conradsen, Allan Aasbjerg Nielsen and Henning Skriver (2016). Determining the points of change in time series of polarimetric SAR data. IEEE Transactions on Geoscience and Remote Sensing 54(5), 3007-3024\n",
    "\n",
    "http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6825\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "ls imagery/southyorkshire"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%run scripts/dispms -f imagery/southyorkshire/T20191008_warp.tif"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%run scripts/enlml -d [1000,600,400,400] imagery/southyorkshire/T20191002.tif"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### ENL = 5.8"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "!scripts/run_sar_seqQ.sh T2019 imagery/southyorkshire/ 5.8 0.01"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%run scripts/dispms -f imagery/southyorkshire/dualpol_cmap.tif -c -d [200,200,400,400]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
